{extend name="Public:base" /}
{block name="main"}
<main>
	<!-- breadcrumb-area-start -->
	<section class="breadcrumb-area" data-background="img/bg/page-title.png">
		<div class="container">
			<div class="row">
				<div class="col-xl-12">
					<div class="breadcrumb-text text-center">
						<h1>Registeration</h1>
						<ul class="breadcrumb-menu">
							<li><a href="index.html">home</a></li>
							<li><span>Registeration</span></li>
						</ul>
					</div>
				</div>
			</div>
		</div>
	</section>
	<!-- breadcrumb-area-end -->

	<!-- login Area Strat-->
	<section class="login-area pt-100 pb-100">
		<div class="container">
			<div class="row">
				<div class="col-lg-8 offset-lg-2">
					<div class="basic-login">
						<h3 class="text-center mb-60">Register From Here</h3>
						<form action="#">
							<label for="name">Email Address Or Phone <span>**</span></label>
							<input id="name" type="text" placeholder="Enter Username or Email address..." />
							<label for="pass">Password <span>**</span></label>
							<input id="pass" type="password" placeholder="Enter password..." />
							<label for="tel">Phone <span>**</span></label>
							<input id="tel" type="text" placeholder="Enter Phone..." />
							<label for="email">Email <span>**</span></label>
							<input id="email" type="text" placeholder="Enter Email..." />
							<label for="bus_name">Email Address Or Phone <span>**</span></label>
							<input id="bus_name" type="text" placeholder="Enter bus_name..." />
							<label for="license">License <span>**</span></label>
							<!-- 图片上传 -->
							<input id="rightupload" type="file" name="" value="" onchange="btnUploadFile(event)" />
							<img id="usrightimg" style="max-height: 10rem;" src="" alt="">
							<!-- 图片上传 -->
							<input id="license" type="hidden" placeholder="" />
						</form>
						<button style="margin-top: 1rem;" type="button" class="btn theme-btn w-100" onclick="registerWholesale()">Register
							Now</button>
					</div>
				</div>
			</div>
		</div>
	</section>
	<!-- login Area End-->
</main>
{/block}
{block name="script"}
<script>
	//获取产品信息
	function registerWholesale() {
		let ajax = new Services();
		var data = {
			user_id:0,
			name: $("#name").val(),
			pass: $("#pass").val(),
			tel: $("#tel").val(),
			email: $("#email").val(),
			bus_name: $("#bus_name").val(),
			license: $("#license").val(),
		}
		ajax.postExample("/index/registerWholesale", data,
			function(res) {
				if (res.code == 200) {
					//注册成功
					TS.successAlert("register success");
					//保存token,user,username
					$.cookie("token", res.data.token, {
						path: '/'
					});
					$.cookie("username", res.data.user.userName, {
						path: '/'
					});
					$.cookie("is_wholesale", res.data.user.is_wholesale, {
						path: '/'
					});
					$.cookie("user", JSON.stringify(res.data.user), {
						path: '/'
					});
					//保存信息之后跳转到首页
					window.location.href = "/";
				} else {
					//错误提示
					TS.errorAlert(res.error);
				}
			},
			function(e) {
				console.log("222", e);
			}
		)
	}
	//图片转base64
	$('#rightupload').change(function() {
		run(this, function (data) { 
			$('#usrightimg').attr('src', data); 
			$('#license').val(data); 
		}); 
	})
	function run(input_file, get_data) {
		/*input_file：文件按钮对象*/
		/*get_data: 转换成功后执行的方法*/
		if (typeof(FileReader) === 'undefined') {
			alert("抱歉，你的浏览器不支持 FileReader，不能将图片转换为Base64，请使用现代浏览器操作！");
		} else {
			try {
				/*图片转Base64 核心代码*/
				var file = input_file.files[0];
				//这里我们判断下类型如果不是图片就返回 去掉就可以上传任意文件 
				if (!/image\/\w+/.test(file.type)) {
					alert("请确保文件为图像类型");
					return false;
				}
				var reader = new FileReader();
				reader.onload = function() {
					get_data(this.result);
				}
				reader.readAsDataURL(file);
			} catch (e) {
				alert('图片转Base64出错啦！' + e.toString())
			}
		}
	}
</script>
{/block}
